local jLootz = jLootz
local lib = jLootz:GetModule("Frame")

local frame = CreateFrame("Frame", nil, UIParent)

frame:SetFrameStrata("DIALOG")
frame:EnableMouse(true)
frame:SetMovable(true)
frame:SetToplevel(true)
frame:SetBackdrop({
	bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
	tile = true,
	edgeSize = 16,
	tileSize = 32,
	insets = {
		left = 5,
		right = 5,
		top = 5,
		bottom = 5
	}
})

local close = CreateFrame("Button", nil, frame)
frame.close = close

local function CreateTexture (parent, path, blend)
	local t = parent:CreateTexture()
	t:SetAllPoints(parent)
	t:SetTexture(path)
	if blend then
		t:SetBlendMode(blend)
	end
	return t
end

close:SetNormalTexture(CreateTexture(close, "Interface\\Buttons\\UI-Panel-MinimizeButton-Up"))
close:SetPushedTexture(CreateTexture(close, "Interface\\Buttons\\UI-Panel-MinimizeButton-Down"))
close:SetHighlightTexture(CreateTexture(close, "Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight", "ADD"))
close:SetScript("OnClick", function (self)
	self:GetParent():Hide()
end)

close:SetWidth(32)
close:SetHeight(32)
close:ClearAllPoints()
close:SetPoint("TOPRIGHT", 1, 0)

local report = CreateFrame("Button", nil, frame)
frame.report = report

local function CreateTexture (parent, path, blend)
	local t = parent:CreateTexture()
	t:SetAllPoints(parent)
	t:SetTexture(path)
	if blend then
		t:SetBlendMode(blend)
	end
	return t
end

-- report:SetNormalTexture(CreateTexture(report, "Interface\\Buttons\\UI-Panel-MinimizeButton-Up"))
-- report:SetPushedTexture(CreateTexture(report, "Interface\\Buttons\\UI-Panel-MinimizeButton-Down"))
report:SetHighlightTexture(CreateTexture(report, "Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight", "ADD"))
report:SetScript("OnClick", function (self)
	lib:ReportLoots()
end)

report:SetWidth(64)
report:SetHeight(32)
do
	local t = report:CreateFontString(nil, "BORDER")
	t:SetFontObject("GameFontDarkGraySmall")
	t:SetJustifyH("CENTER")
	report:SetFontString(t)
end
report:SetText("Report...")
report:ClearAllPoints()
report:SetPoint("BOTTOMRIGHT", -4, -4)


frame:SetScript("OnMouseDown", function(self)
	self:StartMoving()
end)

frame:SetScript("OnMouseUp", function(self)
	self:StopMovingOrSizing()
	lib:SaveFramePosition(self:GetLeft(), self:GetTop())
end)

frame:SetScript("OnShow", function (self)
	if IsFishingLoot() then
		PlaySound()
	end
	lib:Update()
end)

frame:SetScript("OnHide", function (self)
	CloseLoot()
end)

frame:SetScript("OnEvent", function (self, event, ...)
	local handler = lib[event]
	if handler then
		handler(lib, ...)
	end
end)

frame:RegisterEvent("LOOT_OPENED")
frame:RegisterEvent("LOOT_SLOT_CLEARED")
frame:RegisterEvent("LOOT_CLOSED")
frame:RegisterEvent("LOOT_BIND_CONFIRM")

frame:SetPoint("CENTER")
frame:SetWidth(100)
frame:SetHeight(100)
frame:Hide()
lib.frame = frame
-- /print jLootz:GetModule("Frame").frame:Show()
